package com.gnomon.epsos.servlet;

import java.io.*;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.PortletRequest;
import javax.servlet.http.*;
import javax.servlet.*;

import org.apache.log4j.Logger;
import org.opensaml.saml2.core.Assertion;

import com.gnomon.LiferayUtils;
import com.gnomon.epsos.model.EpsosDocument;
import com.gnomon.epsos.service.EpsosHelperService;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.portlet.PortletProps;

import epsos.openncp.protocolterminator.ClientConnectorConsumer;
import epsos.openncp.protocolterminator.clientconnector.DocumentId;
import epsos.openncp.protocolterminator.clientconnector.EpsosDocument1;
import epsos.openncp.protocolterminator.clientconnector.GenericDocumentCode;

public class PDFServlet extends HttpServlet {
	
	private static Logger log = Logger.getLogger("pdfServlet");
	
	public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {

		byte[] pdf = null;
		log.info("getting pdf document");
		try {
			String uuid = req.getParameter("uuid");
			String repositoryId = req.getParameter("repositoryid");
			String hcid = req.getParameter("hcid");

			log.info("Retrieving PDF document");
			log.info("uuid: " + uuid);
			log.info("repositoryId: " + repositoryId);
			log.info("hcid: " + hcid);
			
			EpsosDocument selectedEpsosDocument = new EpsosDocument();
			String serviceUrl = com.liferay.util.portlet.PortletProps
					.get("client.connector.service.url");
			ClientConnectorConsumer clientConectorConsumer = new ClientConnectorConsumer(
					serviceUrl);

			HttpSession session = req.getSession();

			Assertion hcpAssertion = (Assertion) session.getAttribute("hcpAssertion");
			Assertion trcAssertion = (Assertion) session.getAttribute("trcAssertion");
			String selectedCountry = (String) session.getAttribute("selectedCountry");

			DocumentId documentId = DocumentId.Factory.newInstance();
			documentId.setDocumentUniqueId(uuid);
			documentId.setRepositoryUniqueId(repositoryId);
			
			GenericDocumentCode classCode = GenericDocumentCode.Factory.newInstance();
			String docType = req.getParameter("docType");
			if (docType.equals("ep"))
			{
			classCode.setNodeRepresentation(PortletProps.get("prescription.classCode"));
			classCode.setSchema(PortletProps.get("classcode.schema"));
			classCode.setValue(PortletProps.get("prescription.value"));
			}
			if (docType.equals("ps"))
			{
			classCode.setNodeRepresentation(PortletProps.get("patient.summary.classCode"));
			classCode.setSchema(PortletProps.get("patient.summary.schema"));
			classCode.setValue(PortletProps.get("patient.summary.value"));
			}
				
			EpsosDocument1 eps = clientConectorConsumer.retrieveDocument(
					hcpAssertion, trcAssertion, selectedCountry, documentId,
					hcid,classCode);
			selectedEpsosDocument.setAuthor(eps.getAuthor()+"");
			selectedEpsosDocument.setCreationDate(eps.getCreationDate());
			selectedEpsosDocument.setDescription(eps.getDescription());
			selectedEpsosDocument.setTitle(eps.getTitle());

			String xmlfile = new String(eps.getBase64Binary(), "UTF-8");
			
			pdf = EpsosHelperService.extractPdfPartOfDocument(xmlfile.getBytes());
			
			res.setContentType("application/pdf");  
		   
			res.setHeader("Content-Disposition","inline; filename=architect.pdf");  
			res.setHeader("Cache-Control", "no-cache");  
			res.setDateHeader("Expires", 0);  
			res.setHeader("Pragma", "No-cache");   

			OutputStream OutStream = res.getOutputStream();  
			OutStream.write(pdf);  
			OutStream.flush();  
			OutStream.close(); 
			
		} catch (Exception ex) {
			res.setContentType("text/html");  
			   
			res.setHeader("Cache-Control", "no-cache");  
			res.setDateHeader("Expires", 0);  
			res.setHeader("Pragma", "No-cache");   

			OutputStream OutStream = res.getOutputStream();  
			OutStream.write(ex.getMessage().getBytes());  
			OutStream.flush();  
			OutStream.close(); 
			ex.printStackTrace();
		}

  
  
  }
}